<script>
	import LoginUtil from 'common/LoginUtil.js'
	import constant from '@/common/constant.js'
	
	// import TencentCloudChat from '@tencentcloud/chat';
	// import { TUIChatKit, genTestUserSig } from "./TUIKit";
	// import { TUILogin } from "@tencentcloud/tui-core";
	import IMUtil from './common/IMUtil';
	IMUtil.init()

	export default {
		globalData: {
			webUrl: '',
			webTitle: '',
			chatUserId: '',
			chatTxCode: '',
			callerId: '',
			timeCall: {},
			versionName: '',
			openTicketMin: false,
			ticketStartTime: 0,
			ticketTotalTime: 180,
			deviceModel: '',
		},
		onLaunch: function() {
			uni.$online = constant.online;
			uni.getSystemInfo({
				success: (e) => {
					this.globalData.deviceModel = e.brand + '/' + e.deviceModel;
				}
			})
			this.checkUpdate();
			this.isLogin();
			this.loginIM();
			// 离线推送：打开通知栏
			IMUtil.initOfflinePushListener()
		},

		methods: {
			isLogin() {
				const login = uni.getStorageSync("login");
				if (!login) {
					// #ifdef APP-PLUS
					LoginUtil.loginOnce()
					// #endif
				}
				// #ifdef MP-WEIXIN
					this.wxLogin()
				// #endif
			},
			async wxLogin(){
				console.log("微信登录-开始")
				const openId = uni.getStorageSync('openId');
				console.log("openId:"+openId)
				if(openId){
					LoginUtil.wxLogin(openId);
				}else{
					this.getOpenId()
				}
			},
			getOpenId(){
				console.log("获取openId-开始")
				uni.login({
						"provider": "weixin",
						"onlyAuthorize": true, // 微信登录仅请求授权认证
						success: async (event) => {
							const { code } = event
							let url = constant.baseUrl+'/api/appWxLogin/loginWx?appName=zy&jsCode='+code;
							console.log("获取openId-code："+code)
							console.log("获取openId-url："+url)
							uni.request({
								url: url,
								success: (res) => {
									console.log('获得openId完成:' + JSON.stringify(res));
									if(res.data.data.openid){
										uni.setStorageSync('openId', res.data.data.openid);
										LoginUtil.wxLogin(res.data.data.openid);
									}
								}
							});
						},
					})
			},
			
			loginIM() {
				IMUtil.login()
			},
			

			//检查更新
			checkUpdate() {
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.versionName = info.version;
				});
				let urlstr = "https://zhiyuapp.oss-cn-fuzhou.aliyuncs.com/uniapp_android/testConfig.json?t=" + new Date().getTime();
				uni.request({
					url: urlstr,
					success: (res) => {
						uni.setStorageSync('vipManage', res.data.vipManage);
						uni.setStorageSync('ticketPrice', res.data.ticketPrice);
						if (res.data.ticketTime) {
							this.globalData.ticketTotalTime = res.data.ticketTime
						}
						if (res.data.open == "1" && res.data.vss != this.versionName) {
							this.install(res.data.wgtUrl);
						}
					}
				})
				// #endif
			},

			//wgt下载安装
			install(wgtUrl) {
				// #ifdef APP-PLUS
				plus.downloader.createDownload(wgtUrl, {
					filename: "_doc/update/"
				}, (d, status) => {
					if (status == 200) {
						plus.runtime.install(d.filename, {
							force: true
						}, function() {
							plus.nativeUI.closeWaiting();
							plus.runtime.restart();
						}, function(e) {
							plus.nativeUI.closeWaiting();
						});
					} else {
						plus.nativeUI.closeWaiting();
					}
				}).start();
				// #endif
			},

			toTicket() {
				if (0 == this.globalData.ticketStartTime) {
					let timeId = setInterval(() => {
						// console.log('ticket--start', this.globalData.ticketStartTime);
						this.globalData.ticketStartTime++
						if (this.globalData.ticketStartTime == this.globalData.ticketTotalTime) {
							clearInterval(timeId)
							uni.setStorageSync("ticketOver" + uni.getStorageSync('userId'), true)
							this.globalData.ticketStartTime = 0
							this.globalData.openTicketMin = true
							uni.reLaunch({
								url: '/pages/tab/home/home',
							});
						}
					}, 1000)
				}
			}

		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	@import '@/common/uni.css';

	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>